#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @File : edit_blogs_page.py
# @Autor: FY
# @Date: 2021/10/12
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from pages.base_page import BasePage
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options


class EditBlogsPage(BasePage):
    save_locator = (By.XPATH, '//button[@cnbellocator="saveChangesBtn"]')

    def goto_BasePage(self):
        self.driver.get("http://www.baidu.com")
        sleep(5)

    def click_save_btn(self):
        from pages.save_sucess_page import SaveSuccessPage

        self.find_and_click_btn(self.save_locator)
        return SaveSuccessPage(self.driver)

    def is_edit_blogs_page(self):
        return self.find_and_scroll_element(self.save_locator)

    def scroll_to_editarea_to_bottom(self):
        iframe_name = 'Editor_Edit_EditorBody_ifr'
        WebDriverWait(self.driver, 10).until(lambda x: self.driver.find_element(By.TAG_NAME, "iframe"))
        self.driver.switch_to.frame(iframe_name)
        edit_area_size = self.driver.get_window_size()
        height = edit_area_size["height"]
        print(height)

        js_script = 'var scrollHeight = document.body.scrollHeight;  window.scrollTo(0,scrollHeight)'
        print(js_script)
        self.driver.execute_script(js_script)

        self.driver.switch_to.default_content()
        return self
